

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MemberInsert
 */
public class MemberInsert extends HttpServlet {
	private static final long serialVersionUID = 1L;
     
	private static final String CLASS_NAME = "oracle.jdbc.driver.OracleDriver";
	private static final String CONNECTION_URL = "jdbc:oracle:thin:scott/tiger@127.0.0.1:1521:xe";

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MemberInsert() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		
		PrintWriter out = response.getWriter();
		out.println("<html><body>");
		
		if(id.equals("") || pw.equals("")){
			out.println("ID 또는 Password가 입력되지 않았습니다.<br/>");
			out.println("<a href=/edu/memberinsert.html>이전페이지로...</a>");
			out.println("</body></html>");
			
			return;
		}
		
		Connection con = null;
		
		try {
			Class.forName(CLASS_NAME);
			
			con = DriverManager.getConnection(CONNECTION_URL);
			
			String sql = "select * from member where id = ?";
	
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setString(1, id);
			
			ResultSet rs = ps.executeQuery();
			
			if(rs.next()){
				out.println("ID가 이미 존재합니다.<br/>");
				out.println("<a href=/edu/memberinsert.html>이전페이지로...</a>");
				out.println("</body></html>");
				
				return;
			}
			
			sql = "insert into member values(?, ?)";
			
			ps = con.prepareStatement(sql);
			ps.setString(1, id);
			ps.setString(2, pw);
			
			ps.executeUpdate();
			
			out.println("ID : " + id + "가 가입되었습니다.");
			out.println("</body></html>");
				
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally{
			try {
				if(con != null && !con.isClosed()){
					con.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		
	}

}
